package com.kexio.common.dto;

import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;

/**
 * 分页请求参数
 * 
 * @author Kexio Team
 * @since 1.0.0
 */
public class PageRequest {
    
    /**
     * 默认页码
     */
    public static final int DEFAULT_PAGE = 1;
    
    /**
     * 默认页大小
     */
    public static final int DEFAULT_SIZE = 10;
    
    /**
     * 最大页大小
     */
    public static final int MAX_SIZE = 100;
    
    /**
     * 当前页码 (从1开始)
     */
    @Min(value = 1, message = "页码必须大于0")
    private Integer page = DEFAULT_PAGE;
    
    /**
     * 每页大小
     */
    @Min(value = 1, message = "每页大小必须大于0")
    @Max(value = MAX_SIZE, message = "每页大小不能超过100")
    private Integer size = DEFAULT_SIZE;
    
    /**
     * 排序字段
     */
    private String sort;
    
    /**
     * 排序方向 (asc/desc)
     */
    private String order = "asc";
    
    /**
     * 搜索关键词
     */
    private String keyword;
    
    public PageRequest() {
    }
    
    public PageRequest(Integer page, Integer size) {
        this.page = page == null || page < 1 ? DEFAULT_PAGE : page;
        this.size = size == null || size < 1 ? DEFAULT_SIZE : Math.min(size, MAX_SIZE);
    }
    
    public PageRequest(Integer page, Integer size, String sort, String order) {
        this(page, size);
        this.sort = sort;
        this.order = order;
    }
    
    /**
     * 获取偏移量 (用于SQL OFFSET)
     */
    public Integer getOffset() {
        return (page - 1) * size;
    }
    
    /**
     * 获取限制数量 (用于SQL LIMIT)
     */
    public Integer getLimit() {
        return size;
    }
    
    /**
     * 创建分页请求
     */
    public static PageRequest of(Integer page, Integer size) {
        return new PageRequest(page, size);
    }
    
    /**
     * 创建带排序的分页请求
     */
    public static PageRequest of(Integer page, Integer size, String sort, String order) {
        return new PageRequest(page, size, sort, order);
    }
    
    // Getters and Setters
    public Integer getPage() {
        return page;
    }
    
    public void setPage(Integer page) {
        this.page = page == null || page < 1 ? DEFAULT_PAGE : page;
    }
    
    public Integer getSize() {
        return size;
    }
    
    public void setSize(Integer size) {
        this.size = size == null || size < 1 ? DEFAULT_SIZE : Math.min(size, MAX_SIZE);
    }
    
    public String getSort() {
        return sort;
    }
    
    public void setSort(String sort) {
        this.sort = sort;
    }
    
    public String getOrder() {
        return order;
    }
    
    public void setOrder(String order) {
        this.order = order;
    }
    
    public String getKeyword() {
        return keyword;
    }
    
    public void setKeyword(String keyword) {
        this.keyword = keyword;
    }
    
    @Override
    public String toString() {
        return "PageRequest{" +
                "page=" + page +
                ", size=" + size +
                ", sort='" + sort + '\'' +
                ", order='" + order + '\'' +
                ", keyword='" + keyword + '\'' +
                '}';
    }
}
